package com.lcp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcp.entity.LectureRegistration;
import com.lcp.mapper.LectureRegistrationMapper;
import com.lcp.service.LectureRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class LectureRegistrationServiceImpl implements LectureRegistrationService {

    @Autowired
    private LectureRegistrationMapper lectureRegistrationMapper;

    @Override
    public LectureRegistration getById(Long id) {
        return lectureRegistrationMapper.selectById(id);
    }

    @Override
    public IPage<LectureRegistration> pageByLectureId(Long lectureId, Integer pageNum, Integer pageSize) {
        QueryWrapper<LectureRegistration> wrapper = new QueryWrapper<>();
        wrapper.eq("lecture_id", lectureId)
            .orderByDesc("registration_time");
            
        return lectureRegistrationMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public List<LectureRegistration> listByUserId(Long userId) {
        QueryWrapper<LectureRegistration> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
            .orderByDesc("registration_time");
            
        return lectureRegistrationMapper.selectList(wrapper);
    }

    @Override
    @Transactional
    public LectureRegistration create(LectureRegistration registration) {
        registration.setCreateTime(LocalDateTime.now());
        registration.setUpdateTime(LocalDateTime.now());
        registration.setRegistrationTime(LocalDateTime.now());
        registration.setStatus("REGISTERED");
        
        lectureRegistrationMapper.insert(registration);
        return registration;
    }

    @Override
    @Transactional
    public LectureRegistration update(LectureRegistration registration) {
        registration.setUpdateTime(LocalDateTime.now());
        lectureRegistrationMapper.updateById(registration);
        return registration;
    }

    @Override
    @Transactional
    public void delete(Long id) {
        lectureRegistrationMapper.deleteById(id);
    }

    @Override
    public boolean isRegistered(Long lectureId, Long userId) {
        QueryWrapper<LectureRegistration> wrapper = new QueryWrapper<>();
        wrapper.eq("lecture_id", lectureId)
            .eq("user_id", userId)
            .eq("status", "REGISTERED");
            
        return lectureRegistrationMapper.selectCount(wrapper) > 0;
    }

    @Override
    @Transactional
    public void cancelRegistration(Long lectureId, Long userId) {
        QueryWrapper<LectureRegistration> wrapper = new QueryWrapper<>();
        wrapper.eq("lecture_id", lectureId)
            .eq("user_id", userId)
            .eq("status", "REGISTERED");
            
        LectureRegistration registration = lectureRegistrationMapper.selectOne(wrapper);
        if (registration != null) {
            registration.setStatus("CANCELLED");
            registration.setUpdateTime(LocalDateTime.now());
            lectureRegistrationMapper.updateById(registration);
        }
    }

    @Override
    @Transactional
    public void markAsAttended(Long lectureId, Long userId) {
        QueryWrapper<LectureRegistration> wrapper = new QueryWrapper<>();
        wrapper.eq("lecture_id", lectureId)
            .eq("user_id", userId)
            .eq("status", "REGISTERED");
            
        LectureRegistration registration = lectureRegistrationMapper.selectOne(wrapper);
        if (registration != null) {
            registration.setStatus("ATTENDED");
            registration.setUpdateTime(LocalDateTime.now());
            lectureRegistrationMapper.updateById(registration);
        }
    }

    @Override
    @Transactional
    public void submitFeedback(Long lectureId, Long userId, String feedback, Integer rating) {
        QueryWrapper<LectureRegistration> wrapper = new QueryWrapper<>();
        wrapper.eq("lecture_id", lectureId)
            .eq("user_id", userId)
            .eq("status", "ATTENDED");
            
        LectureRegistration registration = lectureRegistrationMapper.selectOne(wrapper);
        if (registration != null) {
            registration.setFeedback(feedback);
            registration.setRating(rating);
            registration.setUpdateTime(LocalDateTime.now());
            lectureRegistrationMapper.updateById(registration);
        }
    }
} 